#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sklearn
import numpy as np
import pandas as pd
np.random.seed(0)
def main():
    # 1. reading data
    test_data = pd.read_table("acetylation_test.txt", sep='\t', header=None, names=['ID', 'Sequence', 'TrueState'])
    encode={"A":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        "R":[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        "N":[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        "D":[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        "C":[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        "Q":[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
        "E":[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
        "G":[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],
        "H":[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
        "I":[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
        "L":[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0],
        "K":[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
        "M":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
        "F":[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0],
        "P":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
        "S":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
        "T":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
        "W":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
        "Y":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
        "V":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
    }
    data_test = []
    xtest, ttest = [], []
    for row in test_data.iterrows():
        data_test.append([row[1][1][0:3], True if row[1][2] == "Ac" else False])
    for i, v in enumerate(data_test):
        code = []
        for char in v[0]:
            code += encode[char]
        xtest.append(code)
        ttest.append(1 if v[1] is True else 0)        
    xtest=np.asarray(xtest,dtype=np.float32)
    ttest=np.asarray(ttest,dtype=np.int32)
    # 2. standardization of data
    scaler=sklearn.externals.joblib.load("scaler_lr.pkl")
    xtest=scaler.transform(xtest)
    # 3. reading predictor
    predictor=sklearn.externals.joblib.load("predictor_lr.pkl")
    # 4. evaluating the performance of the predictor on the test dataset
    liprediction=predictor.predict(xtest)
    table=sklearn.metrics.confusion_matrix(ttest,liprediction)
    tn,fp,fn,tp=table[0][0],table[0][1],table[1][0],table[1][1]
    print("TPR\t{0:.3f}".format(tp/(tp+fn)))
    print("SPC\t{0:.3f}".format(tn/(tn+fp)))
    print("PPV\t{0:.3f}".format(tp/(tp+fp)))
    print("ACC\t{0:.3f}".format((tp+tn)/(tp+fp+fn+tn)))
    print("MCC\t{0:.3f}".format((tp*tn-fp*fn)/((tp+fp)*(tp+fn)*(tn+fp)*(tn+fn))**(1/2)))
    print("F1\t{0:.3f}".format((2*tp)/(2*tp+fp+fn)))
if __name__ == '__main__':
    main()